﻿Imports System.Data.Common
Imports System.Xml

Public MustInherit Class WallpaperService

  Protected _connection As DbConnection
  Protected _config As Configuration = Configuration.GetObject

  Public Sub New(ByVal connection As DbConnection)
    _connection = connection
  End Sub

  Public ReadOnly Property GetListOfNew(Optional ByVal rss As String = "http://www.skins.be/feeds/de/skins.xml") As DownloadabeWallpaper()
    Get
      Dim r() As DownloadabeWallpaper
      Dim links As New ArrayList
      Dim lastvalue As String = ""

      Dim xmldoc As New XmlDocument
      Try
        xmldoc.Load(rss)
      Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler")
        ReDim r(-1)
        Return r
      End Try
      Dim xmlnr As New XmlNodeReader(xmldoc)
      While xmlnr.Read()
        Select Case xmlnr.NodeType
          Case Xml.XmlNodeType.Text
            If xmlnr.Value.StartsWith("http:") And xmlnr.Value.IndexOf("wallpaper.skins.be") > 0 Then
              If xmlnr.Value <> lastvalue Then
                lastvalue = xmlnr.Value
                If CheckOrgaIDexists(xmlnr.Value.Split("//")(4), _config.Resolution) = False Then
                  links.Add(xmlnr.Value)
                End If
              End If
            End If
        End Select
      End While

      ReDim r(links.Count - 1)
      For i As Integer = 0 To links.Count - 1 Step 1
        Dim splitted() As String = DirectCast(links(i), String).Split("//")
        If splitted.Length = 7 Then
          r(i) = GetNewDownloadableWallpaper
          r(i).Resolution = _config.Resolution
          r(i).OrgaID = splitted(4)
          r(i).Name = splitted(3)
          r(i).Filename = splitted(3) & "-RESOLUTION-" & splitted(4) & ".jpg"
          r(i).URL = "http://wallpapers.skins.be/" & splitted(3) & "/" & splitted(3) & "-RESOLUTION-" & splitted(4) & ".jpg"
        End If
      Next

      Return r
    End Get
  End Property

  Protected Sub DeleteFromFilesystem(ByVal file As String)
    IO.File.Delete(file)
  End Sub

  Public MustOverride Sub IncrementWallpaperSet(ByRef value As Wallpaper)

  Public MustOverride ReadOnly Property CheckOrgaIDexists(ByVal orga_id As Integer, ByVal resolution As String) As Boolean

  Public MustOverride ReadOnly Property GetWallpaperById(ByVal wallpaper_id As Integer) As Wallpaper

  Public MustOverride ReadOnly Property IsFavoritePic(ByVal w As Wallpaper) As Boolean

  Public MustOverride ReadOnly Property IsFavoriteModel(ByVal w As Wallpaper) As Boolean

  Public MustOverride Sub SetFavoriteModel(ByRef w As Wallpaper)

  Public MustOverride Sub SetFavoritePic(ByRef w As Wallpaper)

  Public MustOverride Sub RemoveFavoriteModel(ByRef w As Wallpaper)

  Public MustOverride Sub RemoveFavoritePic(ByRef w As Wallpaper)

  Public MustOverride ReadOnly Property GetNewDownloadableWallpaper() As DownloadabeWallpaper

  Public MustOverride Sub InsertNewWallpaper(ByRef value As Wallpaper)

  Public MustOverride ReadOnly Property GetMainList(Optional ByVal filter As String = "", Optional ByVal filtername As String = "Filter") As ListInfoWallpaper()

  Public MustOverride Sub DeleteWallpaper(ByVal w As Wallpaper, ByVal filesystem As Boolean)

  Public MustOverride Sub UpdateWallpaper(ByVal w As Wallpaper)

  Public MustOverride Function GetWallpaperByName(ByVal name As String) As List(Of Wallpaper)

  Public MustOverride ReadOnly Property GetListOfWallpaper( ids As list(Of Integer)) As List(Of Wallpaper)
  
  Public MustOverride ReadOnly Property GetModelDetails() as List(Of ModelDetail)
End Class
